package org.txazo.blog.web.action;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.txazo.blog.bean.MemberRegister;
import org.txazo.blog.commons.cache.RegisterCache;
import org.txazo.blog.commons.mail.Mail;
import org.txazo.blog.commons.mail.RegisterConfirmMail;
import org.txazo.blog.service.MemberInfoService;
import org.txazo.blog.service.MemberLoginInfoService;
import org.txazo.blog.util.HttpUtil;
import org.txazo.blog.util.PasswdUtil;
import org.txazo.blog.util.StringUtil;
import org.txazo.blog.util.log.LoggerManager;
import org.txazo.blog.util.md5.MD5;
import org.txazo.blog.vo.MemberInfo;
import org.txazo.blog.vo.MemberLoginInfo;
import org.txazo.blog.web.action.base.BaseAction;

/**
 * 注册
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
@Controller
@Scope("session")
@Namespace("/register")
public class RegisterAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/** 邮箱帐号 */
	private String emailAccount;
	/** 密码 */
	private String password;
	/** 昵称 */
	private String nickname;
	/** 验证码 */
	private String authCode;

	@Autowired
	private MemberInfoService memberInfoService;

	@Autowired
	private MemberLoginInfoService memberLoginInfoService;

	@Autowired
	private RegisterCache registerCache;

	@Autowired
	private JavaMailSender javaMailSender;

	/** 注册页面 */
	@Action(value = "index", results = { @Result(name = SUCCESS, location = "/register/register.jsp") })
	public String index() {
		return SUCCESS;
	}

	/** 用户注册 */
	@Action(value = "register", results = {
			@Result(name = INPUT, location = "/register/register.jsp"),
			@Result(name = SUCCESS, location = "/register/register-mail.jsp") })
	public String register() throws Exception {
		MemberRegister memberRegister = new MemberRegister(emailAccount,
				password, nickname, new Date());
		if (StringUtil.isEmpty(emailAccount, password, nickname)) {
			return ERROR;
		}

		/** 缓存用户注册信息 */
		String e = MD5.md5(emailAccount).toLowerCase();
		registerCache.set(e, memberRegister);

		/** 发送注册验证邮件 */
		Mail mail = new RegisterConfirmMail();
		mail.sendMail(javaMailSender, emailAccount);

		/** 邮箱首页URL */
		String url = "http://mail."
				+ emailAccount.substring(emailAccount.indexOf('@') + 1);
		request.setAttribute("url", url);

		LoggerManager.getRegisterLogger().info(
				HttpUtil.getRemoteIp(request) + " " + emailAccount + " 0");

		return SUCCESS;
	}

	/** 注册邮箱验证 */
	@Action(value = "confirm", results = { @Result(name = SUCCESS, location = "/register/register-success.jsp") })
	public String confirm() {
		String e = (String) request.getParameter("e");

		if (StringUtil.isEmpty(e)) {
			errorMessage = "链接无效";
			return ERROR;
		}

		Object obj = registerCache.get(e);
		if (obj == null) {
			errorMessage = "链接已失效";
			return ERROR;
		}

		MemberRegister memberRegister = (MemberRegister) obj;

		emailAccount = memberRegister.getEmailAccount();
		password = memberRegister.getPassword();

		MemberInfo memberInfo = new MemberInfo(emailAccount,
				memberRegister.getNickname(), 0, memberRegister.getRegTime(),
				memberRegister.getRegTime(), memberRegister.getRegTime());

		int memberId = memberInfoService.addMemberInfo(memberInfo);
		if (memberId == -1) {
			errorMessage = "操作出错";
			return ERROR;
		}

		String cipherKey = PasswdUtil.getCipherKey(emailAccount);
		String cipherText = PasswdUtil.getCipherText(password, cipherKey);

		MemberLoginInfo memberLoginInfo = new MemberLoginInfo(memberId,
				emailAccount, cipherText, cipherKey,
				memberRegister.getRegTime(), memberRegister.getRegTime());

		boolean result = memberLoginInfoService
				.addMemberLoginInfo(memberLoginInfo);

		if (!result) {
			errorMessage = "操作出错";
			return ERROR;
		}

		/** 清除注册缓存 */
		registerCache.delete(e);

		LoggerManager.getRegisterLogger().info(
				HttpUtil.getRemoteIp(request) + " " + emailAccount + " 1");

		return SUCCESS;
	}

	public String getEmailAccount() {
		return emailAccount;
	}

	public void setEmailAccount(String emailAccount) {
		this.emailAccount = emailAccount;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}

}
